package org.finesys.boot.controller;

import org.finesys.common.core.module.R;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping
public class DocController {
    @Value("${fine.name:Fine}")
    private String name;
    @Value("${fine.author:Fine}")
    private String author;
    @Value("${fine.version:1.0.0}")
    private String version;
    @Value("${server.port:9999}")
    private int port;

    @GetMapping("/doc")
    public R<String> docIndex(HttpServletRequest request) {
        // 获取请求的host信息
        String host = request.getRemoteHost();
        //获取请求的协议信息
        String scheme = request.getScheme();
        //获取请求的端口信息
        String gatewayURL = scheme + "://" + host + ":" + port;
        //构建前端页面信息
        String div = "<div class=\"max-w-md mx-auto p-6 bg-white rounded-lg shadow-md mt-10\">" +
                "      <div class=\"text-red-500 text-xl font-bold mb-4\">" + name + " gateway started!</div>" +
                "      <div class=\"space-y-2 mb-6 text-gray-600\">" +
                "        <div><span class=\"mr-2\">author:</span> " + author + "</div>" +
                "        <div><span class=\"mr-2\">version:</span> " + version + "</div>" +
                "      </div>" +
                "      <ul class=\"space-y-3\">" +
                "        <li class=\"p-3 hover:bg-gray-50 rounded transition\">" +
                "          <span class=\"font-medium\">Knife4j: </span>" +
                "          <a href=\"" + gatewayURL + "/doc.html\" " +
                "             target=\"_blank\" rel=\"noopener noreferrer\" " +
                "             class=\"text-blue-600 hover:text-blue-800 underline underline-offset-2 decoration-blue-500\">" +
                "             Knife4j" +
                "          </a>" +
                "        </li>" +
                "        <li class=\"p-3 hover:bg-gray-50 rounded transition\">" +
                "          <span class=\"font-medium\">Document: </span>" +
                "          <a href=\"" + gatewayURL + "/swagger-ui.html\" " +
                "             target=\"_blank\" rel=\"noopener noreferrer\" " +
                "             class=\"text-blue-600 hover:text-blue-800 underline underline-offset-2 decoration-blue-500\">" +
                "             Document" +
                "          </a>" +
                "        </li>" +
                "      </ul>" +
                "    </div>";

        return R.success(div);
    }
}
